package com.cincc.cinccimdemo.common;

import androidx.annotation.NonNull;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @author fengzi
 */
public class TimeUtils {

    /**
     * 得到仿微信日期格式输出
     *
     * @param msgTimeMillis
     * @return
     */
    public static String getMsgFormatTime(String msgTimeMillis) {
        long time = Long.parseLong(msgTimeMillis) / 1000;
        Date msgTime = new Date(time);
        Calendar nowCalendar = Calendar.getInstance();
        nowCalendar.get(Calendar.DAY_OF_YEAR);
        Calendar msgCalendar = Calendar.getInstance();
        msgCalendar.setTimeInMillis(time);

        if (nowCalendar.get(Calendar.YEAR) == msgCalendar.get(Calendar.YEAR)) {
            if (nowCalendar.get(Calendar.DAY_OF_YEAR) == msgCalendar.get(Calendar.DAY_OF_YEAR)) {
                //几点几分
                return getTime(msgTime);
            } else if (msgCalendar.get(Calendar.DAY_OF_YEAR) + 1 == nowCalendar.get(Calendar.DAY_OF_YEAR)) {
                //昨天 几点几分
                return "昨天 " + getTime(msgTime);
            } else if (nowCalendar.get(Calendar.DAY_OF_YEAR) - msgCalendar.get(Calendar.DAY_OF_YEAR) < 7) {
                //一周以内都显示为 周几 几点几分
                return getWeekTime(msgTime);
            } else {
                //几月几号 几点几分
                return getTime1(msgTime);
            }
        } else {
            return getTime2(msgTime);
        }
    }

    //年月日时分秒格式
    public static String getMsgFormatTime1(String time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            Date date = formatter.parse(time);
            // 获取Calendar实例并设置时间为Date对象表示的时间
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);

            long timestamp = calendar.getTimeInMillis() * 1000;
            return getMsgFormatTime(String.valueOf(timestamp));
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 根据时间获取到时周几
     *
     * @param msgTime
     * @return
     */
    private static String getWeekTime(Date msgTime) {
        return getWeekOfDate(msgTime) + " " + getTime(msgTime);
    }

    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    @NonNull
    private static String getTime(Date msgTime) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm", Locale.getDefault());
        return sdf1.format(msgTime);
    }

    @NonNull
    private static String getTime1(Date msgTime) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("MM月dd日 HH:mm", Locale.getDefault());
        return sdf1.format(msgTime);
    }

    @NonNull
    private static String getTime2(Date msgTime) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm", Locale.getDefault());
        return sdf1.format(msgTime);
    }
}
